﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.IO;

namespace RoardersAgentSim
{
    /// <summary>
    /// Log helper class
    /// </summary>
    public class LogHelper
    {
        private static StreamWriter writer;

        private static void InitialiseLog()
        {
            if (writer != null)
                return;
            string directory = ConfigurationManager.AppSettings["log"];
            string fileName = string.Format("LOG_{0}.{1}", DateTime.Now.ToString("yyyyMMddHHmmssffff"), "log");
            writer = new StreamWriter(directory + fileName);
        }
        #region message functions
        /// <summary>
        /// Get the step-info message
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private static string GetStepMessage(string str)
        {
            return GetMSG("INFO", str);
        }
        /// <summary>
        /// Get the error message
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private static string GetErrorMessage(string str)
        {
            return GetMSG("ERROR", str);
        }
        /// <summary>
        /// Get the exception message
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private static string GetExceptionMessage(string str)
        {
            return GetMSG("EXCEPTION", str);
        }
        /// <summary>
        /// Get whatever message
        /// </summary>
        /// <param name="p"></param>
        /// <param name="str"></param>
        /// <returns></returns>
        private static string GetMSG(string p, string str)
        {
            return String.Format("{0} {1} {2}", DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"), p, str);
        }
        #endregion
        /// <summary>
        /// Log the string in the console and if check = true -> it will appear in the log file only if it is configured to use debug mode
        /// </summary>
        /// <param name="message"></param>
        /// <param name="checkDebugMode"></param>
        internal static void LogStep(string message, bool checkDebugMode = false)
        {
            string msg = GetStepMessage(message);
            Console.ForegroundColor = ConsoleColor.Cyan;
            Log(msg, checkDebugMode);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="message"></param>
        internal static void LogError(string message)
        {
            InitialiseLog();
            string msg = GetErrorMessage(message);
            Console.ForegroundColor = ConsoleColor.Red;
            Log(msg);

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ex"></param>
        internal static void LogException(Exception ex)
        {
            string msg = GetExceptionMessage(ex.Source);
            Console.ForegroundColor = ConsoleColor.Yellow;
            Log(msg);
            msg = GetExceptionMessage(ex.Message);
            Console.ForegroundColor = ConsoleColor.Yellow;
            Log(msg);
            msg = GetExceptionMessage(ex.StackTrace);
            Console.ForegroundColor = ConsoleColor.Yellow;
            Log(msg);
        }
        /// <summary>
        /// Log ...by writing aditional information in the file if debug mode is enabled
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="check"></param>
        private static void Log(string msg, bool check = false)
        {
            try
            {
                if ((check && Utils.IsInDebugMode())|| !check)
                {
                    InitialiseLog();
                    if (writer != null)
                        writer.WriteLine(msg);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if ((check && Utils.IsInDebugMode())|| !check)
                    Console.WriteLine(msg);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        internal static void Close()
        {
            if (writer != null)
                writer.Close();
        }

        internal static void Flush()
        {
            if (writer != null)
                writer.Flush();
        }
    }
}
